using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace XFramework
{

    /// <summary>
    /// Ӳ���豸�������乤��
    /// </summary>
    public static class DevicePerformanceUtil
    {
#if !UNITY_WEBGL

        /// <summary>
        /// ��ȡ�豸��������
        /// </summary>
        /// <returns>��������</returns>
        public static DevicePerformanceLevel GetDevicePerformanceLevel()
        {
            if (SystemInfo.graphicsDeviceVendorID == 32902)
            {
                //����
                return DevicePerformanceLevel.Low;
            }
            else //NVIDIAϵ���Կ�(N��)��AMDϵ���Կ�
            {
                //����ĿǰӲ����������ƽ̨�����˲�һ�������б�׼(���������)
                //CPU������
#if UNITY_EDITOR || UNITY_STANDALONE_WIN
                if (SystemInfo.processorCount <= 2)
#elif UNITY_STANDALONE_OSX || UNITY_IPHONE
            if (SystemInfo.processorCount < 2)
#elif UNITY_ANDROID
            if (SystemInfo.processorCount <= 4)
#endif
                {
                    //CPU������<=2�ж�Ϊ�Ͷ�
                    return DevicePerformanceLevel.Low;
                }
                else
                {
                    //�Դ�
                    int graphicsMemorySize = SystemInfo.graphicsMemorySize;
                    //�ڴ�
                    int systemMemorySize = SystemInfo.systemMemorySize;
#if UNITY_EDITOR || UNITY_STANDALONE_WIN
                    if (graphicsMemorySize >= 4000 && systemMemorySize >= 8000)
                        return DevicePerformanceLevel.High;
                    else if (graphicsMemorySize >= 2000 && systemMemorySize >= 4000)
                        return DevicePerformanceLevel.Mid;
                    else
                        return DevicePerformanceLevel.Low;
#elif UNITY_STANDALONE_OSX || UNITY_IPHONE
            if (graphicsMemorySize >= 4000 && systemMemorySize >= 8000)
                    return DevicePerformanceLevel.High;
                else if (graphicsMemorySize >= 2000 && systemMemorySize >= 4000)
                    return DevicePerformanceLevel.Mid;
                else
                    return DevicePerformanceLevel.Low;
#elif UNITY_ANDROID
            if (graphicsMemorySize >= 6000 && systemMemorySize >= 8000)
                    return DevicePerformanceLevel.High;
                else if (graphicsMemorySize >= 2000 && systemMemorySize >= 4000)
                    return DevicePerformanceLevel.Mid;
                else
                    return DevicePerformanceLevel.Low;
#endif
                }
            }
        }

        /// <summary>
        /// �����ֻ������޸���Ŀ����
        /// </summary>
        /// <param name="lowQuality">QualitySettings�ж�ӦLow�ĵȼ�</param>
        /// <param name="midQuality">QualitySettings�ж�ӦMid�ĵȼ�</param>
        /// <param name="highQuality">QualitySettings�ж�ӦHigh�ĵȼ�</param>
        public static void ModifySettingsBasedOnPerformance(int lowQuality, int midQuality, int highQuality)
        {
            DevicePerformanceLevel level = GetDevicePerformanceLevel();
            switch (level)
            {
                case DevicePerformanceLevel.Low:
                    QualitySettings.SetQualityLevel(lowQuality, true);
                    break;
                case DevicePerformanceLevel.Mid:
                    QualitySettings.SetQualityLevel(midQuality, true);
                    break;
                case DevicePerformanceLevel.High:
                    QualitySettings.SetQualityLevel(highQuality, true);
                    break;
            }
        }

        /// <summary>
        /// ���ݻ��������Զ���������
        /// </summary>
        public static void ModifySettingsBasedOnPerformance()
        {
            DevicePerformanceLevel level = GetDevicePerformanceLevel();
            switch (level)
            {
                case DevicePerformanceLevel.Low:
                    SetQualitySettings(QualityLevel.Low);
                    break;
                case DevicePerformanceLevel.Mid:
                    SetQualitySettings(QualityLevel.Mid);
                    break;
                case DevicePerformanceLevel.High:
                    SetQualitySettings(QualityLevel.High);
                    break;
            }
        }

        /// <summary>
        /// ����������Ҫ������������Ҫ�޸ĵ����ã��ɸ��������޸ĵ��и����ַ���ĳһ���������
        /// </summary>
        /// <param name="qualityLevel">�����ȼ�</param>
        public static void SetQualitySettings(QualityLevel qualityLevel)
        {
            switch (qualityLevel)
            {
                case QualityLevel.Low:
                    //ǰ����Ⱦʹ�õ����صƵ������������������Ϊ1
                    QualitySettings.pixelLightCount = 2;
                    //���������ʹ�����ֱ��ʵ��������߲�������(�ͷֱ��������Ĵ���������)��ѡ���� 0_�����ֱ��ʣ�1_1/2�ֱ��ʣ�2_1/4�ֱ��ʣ�3_1/8�ֱ���
                    QualitySettings.globalTextureMipmapLimit = 1;
                    //���ÿ���ݼ���ѡ����?? 0_����������ݣ�2_2����4_4����8_8��������
                    QualitySettings.antiAliasing = 0;
                    //�Ƿ�ʹ���������ں�
                    QualitySettings.softParticles = false;
                    //����ʵʱ����̽�룬��������Ҫ�õ�ʱ���ٴ�
                    QualitySettings.realtimeReflectionProbes = false;
                    //������ã������ƽ����������λ�ö���������������ƺ������ϵͳ�йأ��˴�ûɶ��Ҫ��
                    QualitySettings.billboardsFaceCameraPosition = false;
                    //������Ӳ��Ӱ�Ƿ��
                    QualitySettings.shadows = ShadowQuality.Disable;
                    //���ô�ֱͬ��������VSyncs��ֵ��Ҫ��ÿ֮֡�䴫�ݣ�ʹ��0Ϊ���ȴ���ֱͬ����ֵ������0��1��2��
                    QualitySettings.vSyncCount = 0;
                    break;
                case QualityLevel.Mid:
                    QualitySettings.pixelLightCount = 4;
                    QualitySettings.antiAliasing = 2;
                    QualitySettings.softParticles = false;
                    QualitySettings.realtimeReflectionProbes = true;
                    QualitySettings.billboardsFaceCameraPosition = true;
                    QualitySettings.shadows = ShadowQuality.HardOnly;
                    QualitySettings.vSyncCount = 2;
                    break;
                case QualityLevel.High:
                    QualitySettings.pixelLightCount = 4;
                    QualitySettings.antiAliasing = 8;
                    QualitySettings.softParticles = true;
                    QualitySettings.realtimeReflectionProbes = true;
                    QualitySettings.billboardsFaceCameraPosition = true;
                    QualitySettings.shadows = ShadowQuality.All;
                    QualitySettings.vSyncCount = 2;
                    break;
            }
        }
#endif
    }
    public enum DevicePerformanceLevel
    {
        Low,
        Mid,
        High
    }
    public enum QualityLevel
    {
        Low,
        Mid,
        High
    }

}